package com.zkr.sparecost.service;


import com.zkr.legalperson.model.*;
import com.zkr.planclass.model.*;
import com.zkr.sparecost.enummodel.StatusEnum;
import com.zkr.sparecost.mapper.SpareCostMapper;
import com.zkr.sparecost.model.*;
import com.zkr.sparecost.model.LegalPlantClassVo;
import com.zkr.sparecost.model.XiaLaVo;
import com.zkr.util.AllRecords;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.awt.font.FontRenderContext;
import java.sql.*;
import java.util.*;
import java.util.Date;

/**
 * Created by lenovo on 2020/12/18.
 */
@Service
public class SpareCostServiceImpl implements SpareCostService {


    @Autowired
    private SpareCostMapper spareCostMapper;

    @Override
    public ResultVo queryPage(SpareSelectVo spareSelectVo) {
        //查询数量
        spareSelectVo.setUserId(1);
        int count = spareCostMapper.queryCount(spareSelectVo);

        List<SpareCostClassVo> spareCostClassVoList = spareCostMapper.query(spareSelectVo);
        for (SpareCostClassVo spareCostClassVo : spareCostClassVoList) {

            Integer status = spareCostClassVo.getStatus() == true ? 1 : 0;
            for (StatusEnum c : StatusEnum.values()) {
                if (c.getValue().equals(status)) {
                    spareCostClassVo.setStatusName(c.getName());
                }
            }

        }
        // System.out.println(spareSelectVo);
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(spareSelectVo.getPageIndex());
        allRecords.setDataList(spareCostClassVoList);
        allRecords.setPageSize(spareSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);

    }


    //含税下拉框
    @Override
    public ResultVo queryTaxById(Integer taxId) {
        List<TaxVo> taxList = spareCostMapper.queryTaxById(taxId);

        return ResultBuilderVo.success(taxList);
    }


    //修改启用禁用
    @Override
    @Transactional
    public ResultVo updateStatus(@Valid SpareCostUpdateStatusVo spareCostUpdateStatusVo) {

        SpareCostUpdateStatusVo spareCostClassVo = new SpareCostUpdateStatusVo();
        TimesStatus time = spareCostMapper.queryTimess(spareCostUpdateStatusVo);
        if (time == null){
            return ResultBuilderVo.error("备件价格id不能为空!");
        }
        if (time.getStartTime().before(new Date()) && time.getEndTime().after(new Date())){
            spareCostUpdateStatusVo.setUpdateName("李四");
            spareCostUpdateStatusVo.setUpdateTime(new Date());
            spareCostMapper.updateStatus(spareCostUpdateStatusVo);
        }else {
            return ResultBuilderVo.error("当前备件价格不在有效时间内,不能更改状态");
        }
        return ResultBuilderVo.success();


        /* spareCostMapper.queryStatus(spareCostClassVo);
         if (spareCostClassVo.getEndTime() == (new Date())){
             spareCostUpdateStatusVo.setStatus(true);
         }*/
       /* BeanUtils.copyProperties(spareCostUpdateStatusVo, spareCostClassVo);
        spareCostClassVo.setUpdateTime(new Date());
        spareCostClassVo.setUpdateName("李四");
        spareCostMapper.updateStatus(spareCostClassVo);
        return ResultBuilderVo.success();*/


    }


    //工厂下拉框
    @Override
    public ResultVo queryLegalPlantId(String legalPersonCode) {

        List<LegalPlantClassVo> legalPlantClassVoList = spareCostMapper.queryLegalPlantId(legalPersonCode);
        return ResultBuilderVo.success(legalPlantClassVoList);

    }

    @Override
    public ResultVo xiala() {
        List<CurrencyVo> currencyVos = spareCostMapper.queryCurrency();
        List<PriceType> priceTypes = spareCostMapper.queryPriceType();


        List<XiaLaVo> xiaLaVoList = new ArrayList<>();
        for (StatusEnum c : StatusEnum.values()) {
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setLabel(c.getName());
            xiaLaVo.setValue(c.getValue());
            xiaLaVoList.add(xiaLaVo);
        }


        List<XiaLaVo> currencyVoList = new ArrayList<>();
        for (CurrencyVo c : currencyVos) {
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setLabel(c.getCurrencyCodeName());
            xiaLaVo.setValue(Integer.parseInt(c.getCurrencyCode()));
            currencyVoList.add(xiaLaVo);
        }


        List<XiaLaVo> priceTypeList = new ArrayList<>();
        for (PriceType p : priceTypes) {
            XiaLaVo xiaLaVo = new XiaLaVo();
            xiaLaVo.setLabel(p.getPriceTypeName());
            xiaLaVo.setValue(Integer.parseInt(p.getPriceType()));
            priceTypeList.add(xiaLaVo);
        }

        Map<String, List> map = new HashMap<>();
        map.put("status", xiaLaVoList);
        map.put("priceType", priceTypeList);
        map.put("currencyCode", currencyVoList);
        return ResultBuilderVo.success(map);

    }

    //备件弹窗
    @Override
    public ResultVo querySpare(SpareFrameSelectVo spareFrameSelectVo) {

        //查询数量
        int count = spareCostMapper.querySpareCount(spareFrameSelectVo);
        List<SpareFrame> spareFrameList = spareCostMapper.querySpare(spareFrameSelectVo);

        // System.out.println(spareFrameList);


        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(spareFrameSelectVo.getPageIndex());
        allRecords.setDataList(spareFrameList);
        allRecords.setPageSize(spareFrameSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);

    }

    //添加
    @Override
    @Transactional
    public ResultVo add(@Valid SpareCostAddVo spareCostAddVo) {


        SpareCostClassVo spareCostClassVo = spareCostMapper.queryAddTime(spareCostAddVo);
        if (spareCostClassVo != null) {
            spareCostAddVo.setUpdateName("李四");
            spareCostAddVo.setUpdateTime(new Date());
            spareCostMapper.updateSparePrice(spareCostAddVo);
            return ResultBuilderVo.success();
        }
        //同一个时间段内，同法人、同工厂的同一备件不能有两个价格，如果开始时间相同，则以最新提交的价格覆盖原有的；
        List<TimeList> timeList = spareCostMapper.queryDateTime(spareCostAddVo);
       // List<CodeList> codeLists = spareCostMapper.queryCode(spareCostAddVo);
     /*   if (spareCostClassVo.getEndTime() != null) {
            Date queryEndTime = spareCostMapper.queryEndTime(spareCostClassVo.getEndTime());
            Date queryStartTime = spareCostMapper.queryStartTime(spareCostClassVo.getStartTime());
            if (spareCostClassVo.getEndTime().equals(queryEndTime)) {
                if (spareCostClassVo.getStartTime().equals(queryStartTime)) {
                    Calendar c = Calendar.getInstance();
                    c.setTime(spareCostClassVo.getStartTime());
                    c.add(Calendar.DATE, -1);
                    Date dd = c.getTime();
                    spareCostAddVo.setEndTime(dd);
                }
            }
        }
        for (CodeList lists : codeLists) {
            if (spareCostAddVo.getEndTime().equals(lists.getEndTime())) {
                spareCostAddVo.setUpdateName("李四");
                spareCostAddVo.setUpdateTime(new Date());
                return ResultBuilderVo.success();

            }*/
            for (TimeList list : timeList) {
                if (spareCostAddVo.getEndTime().after(list.getStartTime()) && spareCostAddVo.getStartTime().before(list.getStartTime())) {
                    Calendar c = Calendar.getInstance();
                    c.setTime(list.getStartTime());
                    c.add(Calendar.DATE, -1);
                    Date dd = c.getTime();
                    spareCostAddVo.setEndTime(dd);
                }
                if (spareCostAddVo.getStartTime().after(list.getStartTime()) && spareCostAddVo.getEndTime().before(list.getEndTime())) {
                    Calendar c = Calendar.getInstance();
                    c.setTime(spareCostAddVo.getStartTime());
                    c.add(Calendar.DATE, -1);
                    Date dd = c.getTime();
                    TimeList timeList1 = new TimeList();
                    timeList1.setId(list.getId());
                    timeList1.setEndTime(dd);
                    spareCostMapper.ddupdateTime(timeList1);
                }
                if (spareCostAddVo.getStartTime().before(list.getEndTime()) && spareCostAddVo.getStartTime().after(list.getStartTime())) {
                    Calendar c = Calendar.getInstance();
                    c.setTime(spareCostAddVo.getStartTime());
                    c.add(Calendar.DATE, -1);
                    Date dd = c.getTime();
                    TimeList timeList1 = new TimeList();
                    timeList1.setId(list.getId());
                    timeList1.setEndTime(dd);
                    spareCostMapper.ddupdateTime(timeList1);
                }
                if (spareCostAddVo.getStartTime().before(list.getStartTime()) && spareCostAddVo.getEndTime().after(list.getEndTime())) {
                    Calendar c = Calendar.getInstance();
                    c.setTime(list.getStartTime());
                    c.add(Calendar.DATE, -1);
                    Date dd = c.getTime();
                    spareCostAddVo.setEndTime(dd);
                }

                //新增时判断开始和结束时间给状态赋值
                if (spareCostAddVo.getEndTime() == list.getEndTime()) {
                    if (spareCostAddVo.getStartTime().before(list.getStartTime())) {
                        Calendar c = Calendar.getInstance();
                        c.setTime(list.getStartTime());
                        c.add(Calendar.DATE, -1);
                        Date dd = c.getTime();
                        spareCostAddVo.setEndTime(dd);

                    } else if (spareCostAddVo.getStartTime().after(list.getStartTime())) {
                        Calendar c = Calendar.getInstance();
                        c.setTime(spareCostAddVo.getStartTime());
                        c.add(Calendar.DATE, -1);
                        Date dd = c.getTime();
                        TimeList timeList1 = new TimeList();
                        timeList1.setId(list.getId());
                        timeList1.setEndTime(dd);
                        spareCostMapper.ddupdateTime(timeList1);
                    }
                }
            }
                if (spareCostAddVo.getStartTime().before(new Date()) && spareCostAddVo.getEndTime().after(new Date())){
                    spareCostAddVo.setStatus(true);
        } else if (spareCostAddVo.getStartTime().after(new Date())) {
                spareCostAddVo.setStatus(false);
            }

            spareCostAddVo.setStatus(false);
            spareCostAddVo.setCreatorName("李四");
            spareCostAddVo.setCreatorTime(new Date());
            spareCostAddVo.setUpdateName("李四");
            spareCostAddVo.setUpdateTime(new Date());
            spareCostMapper.insertSpareClass(spareCostAddVo);

        return ResultBuilderVo.success();

        }






    @Override
    @Transactional
    public ResultVo update(@Valid SpareCostUpdate spareCostUpdate) {

        SpareCostClassVo spareCostClassVo = spareCostMapper.queryById(spareCostUpdate.getId());
        if (spareCostClassVo==null) {
            return ResultBuilderVo.error("修改数据不存在");
        }
        if (spareCostClassVo != null){
            spareCostUpdate.setUpdateName("李四");
            spareCostUpdate.setUpdateTime(new Date());
            return ResultBuilderVo.success();
        }

        CodeList codeList = spareCostMapper.queryTimes(spareCostUpdate);

        List<StartTimeVo> startTime = spareCostMapper.selectByCode(codeList);
        for (StartTimeVo startTimeVo :startTime){
            if (codeList.getStartTime().before(startTimeVo.getStartTime()) && spareCostUpdate.getEndTime().after(startTimeVo.getStartTime())){
                Calendar c = Calendar.getInstance();
                c.setTime(startTimeVo.getStartTime());
                c.add(Calendar.DATE,-1);
                Date dd = c.getTime();
                spareCostUpdate.setEndTime(dd);
            }
        }



        SpareCostClassVo spareCostClassVo1 = new SpareCostClassVo();
        BeanUtils.copyProperties(spareCostUpdate, spareCostClassVo1);
        spareCostClassVo1.setUpdateTime(new Date());
        spareCostClassVo1.setUpdateName("李四");
        spareCostMapper.update(spareCostClassVo1);
        return ResultBuilderVo.success();
    }

}
